using System;

using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Updating;
using DevExpress.Xpo;
using DevExpress.Data.Filtering;
using DevExpress.Persistent.BaseImpl;
using DevExpress.ExpressApp.Security;

namespace Ascent.Module
{
    public class Updater : ModuleUpdater
    {
        public Updater(IObjectSpace objectSpace, Version currentDBVersion) : base(objectSpace, currentDBVersion) { }
        public override void UpdateDatabaseAfterUpdateSchema()
        {
            base.UpdateDatabaseAfterUpdateSchema();
        }

        public override void UpdateDatabaseBeforeUpdateSchema()
        {
            //User user1 = ObjectSpace.FindObject<User>(new BinaryOperator("UserName", "admin"));
            //if (user1 == null)
            //{
            //    user1 = ObjectSpace.CreateObject<User>();
            //    user1.UserName = "admin";
            //    user1.FirstName = "admin";
            //    // Set a password if the standard authentication type is used 
            //    user1.SetPassword("password");
            //}

            //User user2 = ObjectSpace.FindObject<User>(new BinaryOperator("UserName", "workjie"));
            //if (user2 == null)
            //{
            //    user2 = ObjectSpace.CreateObject<User>();
            //    user2.UserName = "workjie";
            //    user2.FirstName = "workjie";
            //    // Set a password if the standard authentication type is used 
            //    user2.SetPassword("password");
            //}
            //// If a role with the Administrators name does not exist in the database, create this role 
            //Role adminRole = ObjectSpace.FindObject<Role>(new BinaryOperator("Name", "Administrators"));
            //if (adminRole == null)
            //{
            //    adminRole = ObjectSpace.CreateObject<Role>();
            //    adminRole.Name = "Administrators";
            //}
            //// If a role with the Users name does not exist in the database, create this role 
            //Role userRole = ObjectSpace.FindObject<Role>(new BinaryOperator("Name", "Users"));
            //if (userRole == null)
            //{
            //    userRole = ObjectSpace.CreateObject<Role>();
            //    userRole.Name = "Users";
            //}
            //// Delete all permissions assigned to the Administrators and Users roles 
            //while (adminRole.PersistentPermissions.Count > 0)
            //{
            //    ObjectSpace.Delete(adminRole.PersistentPermissions[0]);
            //}
            //while (userRole.PersistentPermissions.Count > 0)
            //{
            //    ObjectSpace.Delete(userRole.PersistentPermissions[0]);
            //}
            //// Allow full access to all objects to the Administrators role 
            //adminRole.AddPermission(new ObjectAccessPermission(typeof(object), ObjectAccess.AllAccess));
            //// Allow editing the Application Model to the Administrators role 
            //adminRole.AddPermission(new EditModelPermission(ModelAccessModifier.Allow));
            //// Save the Administrators role to the database 
            //adminRole.Save();
            //// Allow full access to all objects to the Users role 
            //userRole.AddPermission(new ObjectAccessPermission(typeof(object), ObjectAccess.AllAccess));
            //// Deny change access to the User type objects to the Users role 
            //userRole.AddPermission(new ObjectAccessPermission(typeof(User),
            //   ObjectAccess.ChangeAccess, ObjectAccessModifier.Deny));
            //// Deny full access to the Role type objects to the Users role 
            //userRole.AddPermission(new ObjectAccessPermission(typeof(Role),
            //   ObjectAccess.AllAccess, ObjectAccessModifier.Deny));
            //// Deny editing the Application Model to the Users role 
            //userRole.AddPermission(new EditModelPermission(ModelAccessModifier.Deny));
            //// Save the Users role to the database 
            //userRole.Save();
            //// Add the Administrators role to the user1 
            //user1.Roles.Add(adminRole);
            //// Add the Users role to the user2 
            //user2.Roles.Add(userRole);
            //// Save the users to the database 
            //user1.Save();
            //user2.Save();

            base.UpdateDatabaseBeforeUpdateSchema();
        }
    }
}
